package oa.base.session;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import oa.base.hibernate.BaseClass;

import org.apache.struts2.ServletActionContext;

public class SessionManger {
	public static final String VALIDATION = "cq.oa.validation";
	public static final String USER = "user";
	public static final String LOGIN_ERROR = "LOGIN_ERROR";
	public static final String IP = "cq.oa.Ip";

	public static Map<String,SessionUser> local = new HashMap<String, SessionUser>();
	
	/**
	 * 添加用户登录信息
	 * @param sessionUser
	 * lilang 
	 * 2013-6-15下午7:16:16
	 */
	public static void addUserInfo(SessionUser sessionUser){
		local.put(getSession().getId(), sessionUser);
		getSession().setAttribute(USER, sessionUser);
	}
	/**
	 * 添加用户登录信息
	 * @param sessionUser
	 * lilang 
	 * 2013-6-15下午7:16:16
	 */
	public static void addUserInfo(HttpSession session,SessionUser sessionUser){
		local.put(session.getId(), sessionUser);
		session.setAttribute(USER, sessionUser);
	}
	/**
	 * 移除用户信息
	 * 
	 * lilang 
	 * 2013-6-15下午7:17:19
	 */
	public static void removeUserInfo(){
		local.remove(getSession().getId());
	}
	/**
	 * 移除用户信息
	 * 
	 * lilang 
	 * 2013-6-15下午7:17:19
	 */
	public static void removeUserInfo(HttpSession session){
		local.remove(session.getId());
	}
	
	public static HttpSession getSession(){
		return ServletActionContext.getRequest().getSession();
	}
	
	public static int getCount(){
		return local.size();
	}
	
	/**
	 * 错误信息写入session
	 * @param session
	 * @param msg
	 * lilang 
	 * 2013-6-15下午7:34:29
	 */
	public static void setErrorMsg(HttpSession session,String msg){
		session.setAttribute(LOGIN_ERROR, msg);
	}
	/**
	 * 获取用户信息
	 * @return
	 * lilang 
	 * 2013-6-15下午7:24:06
	 */
	public static SessionUser getSessionUser(){
		return local.get(getSession().getId());
	}
	
	public static Long getUserId(){
		return getSessionUser().getSid();
	}
	
	public static String getCname(){
		return getSessionUser().getCname();
	}
	
	public static String getDomain(){
		return getSessionUser().getDomain();
	}
	
	public static String getEmail(){
		return getSessionUser().getEmail();
	}
	/**
	 * 当前单位名称
	 * @return
	 * lilang 
	 * 2013-10-13下午6:21:49
	 */
	public static String getOrgCname(){
		return getSessionUser().getOrgCname();
	}
	/**
	 * 部门名称
	 * @return
	 * lilang 
	 * 2013-10-13下午6:21:49
	 */
	public static String getDeptCname(){
		return getSessionUser().getDeptCname();
	}
	/**
	 * 部门Id
	 * @return
	 * lilang 
	 * 2013-10-13下午6:21:49
	 */
	public static Long getDeptId(){
		return getSessionUser().getDeptId();
	}
	
	public static Long getOrgId(){
		Long orgId = getSessionUser().getOrgId();
		if(null==orgId){
			orgId = 0L;
		}
		return orgId;
	}
	
	public static SessionUser getSessionUser(HttpSession session){
		return local.get(session.getId());
	}
	
	public static class SessionUser implements Serializable{
		/**
		 * lilang
		 * 2013-6-15下午7:28:57
		 */
		private static final long serialVersionUID = 9167006199280156302L;
		
		private Long sid;
		/**
		 * 部门名称
		 */
		private String deptCname;
		/**
		 * 单位名称
		 */
		private String companyCname;
		
		
		private Long deptId;
		
		private String cname;
		/**
		 * session ID
		 */
		private String sessionId;
		
		/**
		 * 单位域名后缀
		 */
		private String domain;
		
		/**
		 * 最后登录时间
		 */
		private Date lastLoginTime;
		
		/**
		 * 用户信息
		 */
		private BaseClass user;
		/**
		 * 单位信息
		 */
		private BaseClass org;
		
		private Long orgId;
		
		private String orgCname;
		
		private String email;
		
		
		
		
		public String getDomain() {
			return domain;
		}
		public void setDomain(String domain) {
			this.domain = domain;
		}
		public String getEmail() {
			return email;
		}
		public void setEmail(String email) {
			this.email = email;
		}
		public Long getDeptId() {
			return deptId;
		}
		public void setDeptId(Long deptId) {
			this.deptId = deptId;
		}
		public String getOrgCname() {
			return orgCname;
		}
		public void setOrgCname(String orgCname) {
			this.orgCname = orgCname;
		}
		public String getCname() {
			return cname;
		}
		public void setCname(String cname) {
			this.cname = cname;
		}
		public BaseClass getUser() {
			return user;
		}
		public void setUser(BaseClass user) {
			this.user = user;
		}
		public BaseClass getOrg() {
			return org;
		}
		public void setOrg(BaseClass org) {
			this.org = org;
		}
		public Long getOrgId() {
			return orgId;
		}
		public void setOrgId(Long orgId) {
			this.orgId = orgId;
		}
		public Long getSid() {
			return sid;
		}
		public void setSid(Long sid) {
			this.sid = sid;
		}
		public String getDeptCname() {
			return deptCname;
		}
		public void setDeptCname(String deptCname) {
			this.deptCname = deptCname;
		}
		public String getCompanyCname() {
			return companyCname;
		}
		public void setCompanyCname(String companyCname) {
			this.companyCname = companyCname;
		}
		public String getSessionId() {
			return sessionId;
		}
		public void setSessionId(String sessionId) {
			this.sessionId = sessionId;
		}
		public Date getLastLoginTime() {
			return lastLoginTime;
		}
		public void setLastLoginTime(Date lastLoginTime) {
			this.lastLoginTime = lastLoginTime;
		}
		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result + ((sid == null) ? 0 : sid.hashCode());
			return result;
		}
		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			SessionUser other = (SessionUser) obj;
			if (sid == null) {
				if (other.sid != null)
					return false;
			} else if (!sid.equals(other.sid))
				return false;
			
			if (sessionId == null) {
				if (other.sessionId != null)
					return false;
			} else if (!sessionId.equals(other.sessionId))
				return false;
			
			return true;
		}
	}
}
